package com.jz.subclass.service.impl;

import com.jz.legalplantclass.entity.LegalPlantClassAddVo;
import com.jz.legalplantclass.entity.LegalVO;
import com.jz.legalplantclass.entity.Legalplantclass;
import com.jz.legalplantclass.mapper.LegalplantclassMapper;
import com.jz.planclass.model.PlanClassUpdateVo;
import com.jz.subclass.mapper.SubClassMapper;
import com.jz.subclass.model.*;
import com.jz.subclass.service.SubClassService;
import com.jz.subclass.test.LegalPersonEnum;
import com.jz.subclass.test.SubStatusEnum;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultVo;
import com.jz.util.ResultBuildVo;
import com.sun.xml.internal.ws.api.message.Packet;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * Created by WISE on 2021/12/26.
 */
@Service
public class SubClassServiceImpl implements SubClassService {

    @Autowired
    private SubClassMapper subClassMapper;

    @Override
    public ResultVo firstPage(SubClassSelectVo subClassSelectVo) {
        List<SubClassVo> subClassVoList = subClassMapper.queryData(subClassSelectVo);

        for (SubClassVo subClassVo : subClassVoList) {

            int status = subClassVo.getStatus() ? 1 : 0;
            subClassVo.setStatusName(SubStatusEnum.getNameByStatus(status));
        }

        //查询数量
        int count = subClassMapper.queryCount(subClassSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setDataList(subClassVoList);
        allRecordsVo.setTotalSize(count);
        allRecordsVo.setPageIndex(subClassSelectVo.getPageIndex());
        allRecordsVo.setPageSize(subClassSelectVo.getPageSize());
        allRecordsVo.countPage(count);

        return ResultBuildVo.success(allRecordsVo);
    }

    @Override
    public ResultVo addSubClass(SubClassAddVo subClassAddVo) {
        //验证编码是否存在
        SubClassVo subClassVO = subClassMapper.queryByCode(subClassAddVo.getCode());
        if (subClassVO != null) {
            return ResultBuildVo.error("编码已经存在！");
        }
        //验证名称是否存在
        SubClassVo subClassName = subClassMapper.queryByName(subClassAddVo.getName());
        if (subClassName != null) {
            return ResultBuildVo.error("名称已经存在！");
        }
        //当前时间
        Date nowDate = new Date();
        SubClassVo classVo = new SubClassVo();
        //BeanUtils.copyProperties的作用就是把左边对象的数据  复制 到右边对象的参数上面，注意参数名称和类型必须全部一致，才能转移。
//        classVO.setName(subClassAddVO.getName());

        BeanUtils.copyProperties(subClassAddVo, classVo);
        classVo.setCreateTime(nowDate);
        classVo.setCreateName("通过Session拿到当前用户名称");
        subClassMapper.add(classVo);
        return ResultBuildVo.success();
    }


    @Override
    public ResultVo queryById(Integer id) {

        SubClassVo subClassVo = subClassMapper.queryById(id);

        return ResultBuildVo.success(subClassVo);
    }

    @Override
    public ResultVo updateSubClass(@Valid SubClassUpdateVo subClassUpdateVo) {
        SubClassVo subClassVo = subClassMapper.queryById(subClassUpdateVo.getId());
        if (subClassVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }

        if (subClassVo.getStatus()) {
            return ResultBuildVo.error("只有禁用状态才能修改");

        }
        SubClassVo subClassName = subClassMapper.queryByName(subClassUpdateVo.getName());
        if (subClassName != null && subClassUpdateVo.getId() != subClassName.getId()) {

            return ResultBuildVo.error("修改名称已存在");
        }
        Date nowDate = new Date();
        SubClassVo classVo = new SubClassVo();
        //BeanUtils.copyProperties的作用就是把左边对象的数据  复制 到右边对象的参数上面，注意参数名称和类型必须全部一致，才能转移。
//        classVO.setName(subClassAddVO.getName());
        BeanUtils.copyProperties(subClassUpdateVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        int version =  subClassMapper.update(classVo);
        if (version == 0){

            return ResultBuildVo.error("已被修改，刷新重试");
        }

        return ResultBuildVo.success();

    }

    @Override
    public ResultVo updateStatus(@Valid SubClassUpdateStatusVo subClassUpdateStatusVo) {
        SubClassVo subClassVo = subClassMapper.queryById(subClassUpdateStatusVo.getId());
        if (subClassVo == null) {
            return ResultBuildVo.error("修改对象不存在");
        }
        Date nowDate = new Date();
        SubClassVo classVo = new SubClassVo();
        BeanUtils.copyProperties(subClassUpdateStatusVo, classVo);
        classVo.setUpdateTime(nowDate);
        classVo.setUpdateName("通过Session拿到当前用户名称");
        subClassMapper.updateStatus(classVo);

        return ResultBuildVo.success();

    }


    @Autowired
    private LegalplantclassMapper legalplantclassMapper;

    @Override
    public ResultVo addPlantClass(@Valid LegalPlantClassAddVo legalPlantClassAddVo) {

        Legalplantclass legalplantclass = legalplantclassMapper.queryByLegalPlantCode(legalPlantClassAddVo.getLegalPlantCode());
        if (legalplantclass != null) {

            return ResultBuildVo.error("法人工厂编码已经存在");
        }

//       String legalPlantCode = LegalPersonEnum.getCodeByName(legalPlantClassAddVo.getLegalPersonName());
        Date nowDate = new Date();
        LegalVO legalVO = new LegalVO();
        BeanUtils.copyProperties(legalPlantClassAddVo, legalVO);
        legalVO.setCreateTime(nowDate);
        legalVO.setCreateName("session拿到的名字");
        subClassMapper.add1(legalVO);

        return ResultBuildVo.success();
    }


}

